package com.qdxwx.data.service.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.data.mapper.course.CourseNoteMapper;
import com.qdxwx.models.course.CourseNote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * CourseNoteService
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CourseNoteServiceImpl implements CourseNoteService {

	@Autowired
	private CourseNoteMapper courseNoteMapper;

	@Override
	public int insert(CourseNote courseNote) {
		courseNote.setUpdateTime(LocalDateTime.now());
		if (ObjectUtils.isNull(getCourseNote(courseNote.getUserId(), courseNote.getNodeId()))) {
			// 更新课程的笔记数量
			return courseNoteMapper.createCourseNote(courseNote);
		} else {
			return courseNoteMapper.updateCourseNote(courseNote);
		}
	}

	/**
	 * 根据用户id和节点id查询笔记
	 */
	@Override
	public CourseNote getCourseNote(Long userId, Integer nodeId) {
		return courseNoteMapper.selectCourseNote( userId, nodeId);
	}
	@Override
	public List<CourseNote> getList(CourseNote courseNote, PageRequest page) {
		return courseNoteMapper.selectCourseNoteList(courseNote,page);
	}

	@Override
	public int count(CourseNote courseNote) {
		return courseNoteMapper.countCourseNote(courseNote);
	}

	@Override
	public void delete(Long id) {
		courseNoteMapper.deleteById(id);
	}
}